<template>
  <div class="btns">
    <Button v-auth="'internshipEvaluate'" color="#6C7DA8" @click="comment">评价</Button>
    <Button v-auth="'internshipLeave'"  color="#3FB7B1" @click="quitPost">离岗</Button>
    <Button v-auth="'internshipPersuadeLeave'"  color="error" @click="dieout">劝退</Button>
    <Button v-auth="'internshipDimission'"  color="#8B9FAB" @click="dimission">离职</Button>
    <Button v-auth="'turnProbation'"  color="success" @click="probation">转入试用</Button>
  </div>
  <BasicModal :show="dieOutShow" title="劝退" width="30%" :widthComfirm="true" @comfirm="dieOutComfirm"
    @closeModal="dieOutShow = false">
    <template #content>
      <CommonForm :formList="dieOutFormList" ref="dieOutFormRef"></CommonForm>
    </template>
  </BasicModal>

  <BasicModal :show="dimissionShow" title="离职" width="30%" :widthComfirm="true" @comfirm="dimissionComfirm"
    @closeModal="dimissionShow = false">
    <template #content>
      <CommonForm :formList="dimissionFormList" ref="dimissionFormRef"></CommonForm>
    </template>
  </BasicModal>


  <BasicModal :show="commentShow" title="新增评价" width="30%" :widthComfirm="true" @comfirm="commentComfirm"
    @closeModal="commentShow = false">
    <template #content>
      <CommonForm :formList="commentFormList" ref="commentFormRef"></CommonForm>
    </template>
  </BasicModal>

  <DimissionModal ref="dimissionModalRef" process="实习中" @closeModal="showDimissionModal = false"
    :show="showDimissionModal" @confirm="showDimissionModal = false">
  </DimissionModal>
  <BasicModal :show="probationShow" title="转入试用期" width="30%" :widthComfirm="true" @comfirm="probationComfirm"
    @closeModal="probationShow = false">
    <template #content>
      <CommonForm :formList="formList" ref="formRef"></CommonForm>
    </template>
  </BasicModal>
</template>

<script setup lang='ts'>
import { Button } from "@/components/Button";
import DimissionModal from "../../../../Modals/DimissionModal.vue";  //离岗
import BasicModal from "@/views/ApplicationDetails/Modals/BasicModal.vue";
import type { IndexFormItem } from "@/views/ApplicationDetails/application-type.ts";
import CommonForm from "@/views/applicationDetails/CommonForm.vue";
import { ref } from "vue";
import { message } from "ant-design-vue";
import { internshipProgress, getEntrySchoolInfo, selectCommonConfigFoChildren ,selectUserByRole} from "@/api/application-details/index.ts";
import { comStores } from '@/store/common/comStore';
const store = comStores()
const { userName } = store.userInFor
const showDimissionModal = ref<boolean>(false);
const dimissionModalRef = ref<any>(null);
const probationShow = ref<boolean>(false);
const commentShow = ref<boolean>(false);
const dieOutShow = ref<boolean>(false);
const dimissionShow = ref<boolean>(false)
const emits = defineEmits(['surrender', 'dieOut']);
const getOptions = async () => {
  const { resumeId } = store.currentResume;
  const school = await selectCommonConfigFoChildren({ type: 1 }); //入职分校
  const info = await getEntrySchoolInfo({ resumeId, type: 1 });
  const teacher = await selectUserByRole("MENTOR") //导师
  formList.value[1].options = teacher.data
  if (info.data) {
    const { school, subject, grade ,teacher} = info.data
    formList.value[0].value = [school, subject, grade] as any
    formList.value[1].value = teacher
  }
  formList.value[0].options = school.data
}
const formList = ref<IndexFormItem>([
  { label: "试用", name: "cascader", value: "", component: 'cascader', options: [] },
  { label: "导师", name: "entryTeacher", value: "", component: 'select', options: [] ,required:false},
  { label: "备注", name: "remark", value: "", component: 'textarea', required:false},
])

const commentFormList = ref<IndexFormItem[]>([
  { label: "状态", name: "onDutyStatus", value: "在岗", component: 'select', options: [{ label: "在岗", value: "在岗" }, { label: "离岗", value: "离岗" }] },
  { label: "备注", name: "onDutyAssess", value: "", component: 'textarea', required:false},
])
const dieOutFormList = ref<IndexFormItem[]>([
  { label: "劝退理由", name: "eliminateReason", value: "", component: 'textarea', },
])
const dimissionFormList = ref<IndexFormItem[]>([
  { label: "离职原因", name: "outDutyReason", value: "", component: 'textarea', },
])

const comment = () => {
  console.log('comment 实习中')
  commentShow.value = true
}
const quitPost = () => {
  console.log('quitPost 实习中')
  showDimissionModal.value = true;
}
const dieOutFormRef = ref<any>(null)
const dieout = () => {
  dieOutShow.value = true
  console.log('dieout 实习中')
}
const dieOutComfirm = async () => {
  const res = await dieOutFormRef.value.submitForm();
  if (!res) return;
  const { resumeId, processId } = store.currentResume;
  let param: any = {
    editStatus: 'PUA',
    resumeId: resumeId,
    processId: processId,
    operator: userName,
    eliminateReason:res[0].value
  }
  // res.forEach(el => {
  //   param[el.name] = el.value
  // })
  // param.entrySchool = param.cascader[0]
  // param.entrySubject = param.cascader[1];
  // param.entryGrade = param.cascader[2];
  const data = await internshipProgress(param);
  if (data && data.code == '10000') {
    store.getResumeInfo(resumeId);
    dieOutShow.value = false;
    dieOutFormList.value.forEach(el => {
      el.value = ""
    })
    message.success("操作成功")
  } else {
    message.error(data.msg)
  }


}

const dimissionFormRef = ref<any>(null)
const dimission = async () => {
  dimissionShow.value = true
}

const dimissionComfirm = async () => {
  const res = await dimissionFormRef.value.submitForm();
  if (!res) return;
  const { resumeId, processId } = store.currentResume;
  let param: any = {
    editStatus: 'DEPART',
    resumeId: resumeId,
    processId: processId,
    operator: userName,
    outDutyReason:res[0].value
  }
  const data = await internshipProgress(param);
  if (data && data.code == '10000') {
    store.getResumeInfo(resumeId);
    dimissionShow.value = false;
    dimissionFormList.value.forEach(el => {
      el.value = ""
    })
    message.success("操作成功")
  } else {
    message.error(data.msg)
  }
}

const probation = () => {
  probationShow.value = true;
  getOptions()
  console.log('probation', '实习中')
}
const formRef = ref<any>(null)
const probationComfirm = async () => {
  const { resumeId, processId } = store.currentResume;
  const res = await formRef.value.submitForm();
  if (!res) return;
  let param: any = {
    editStatus: 'TRYOUT',
    resumeId: resumeId,
    processId: processId,
    operator: userName
  }
  res.forEach(el => {
    param[el.name] = el.value
  })
  param.entrySchool = param.cascader[0]
  param.entrySubject = param.cascader[1];
  param.entryGrade = param.cascader[2];
  const data = await internshipProgress(param);
  if (data && data.code == '10000') {
    formList.value.forEach(el => {
      el.value = ""
    })
    store.getResumeInfo(resumeId);
    probationShow.value = false
    message.success("操作成功")
  } else {
    message.error(data.msg)
  }
  // formRef.value.resetFields()
}

const commentFormRef = ref<any>(null)
const commentComfirm = async () => {
  const { resumeId, processId } = store.currentResume;
  const res = await commentFormRef.value.submitForm();
  if (!res) return;
  commentShow.value = false;
  let param: any = {
    editStatus: 'ASSESS',
    resumeId: resumeId,
    processId: processId,
    operator: userName
  }
  res.forEach(el => {
    param[el.name] = el.value
  })
  const data = await internshipProgress(param);
  if (data && data.code == '10000') {
    store.getResumeInfo(resumeId);
    commentFormList.value[1].value =""
    dimissionShow.value = false
    message.success("操作成功")
  } else {
    message.error(data.msg)
  }
}



</script>
<style lang="less" scoped>
.btns {
  display: flex;
  gap: 8px;
  margin-top: 16px;
}
</style>
